package amq.utils.thds;

import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

import amq.utils.JSFAgent;

public class JSFAgentAQThd implements Runnable, MessageListener, ExceptionListener{
	public boolean isStop = false;
	String queueName = null;
	String brokerURI = null;	
	JSFAgent agent;
	
	public JSFAgentAQThd(String qn, String bui, JSFAgent agent)
	{
		this.queueName = qn;
		this.brokerURI = bui;
		this.agent = agent;
	}

	@Override
	public void run() {
		try {
			System.out.printf("\t[Info] Opening Answer Queue(%s)...\n", queueName);
			// Create a ConnectionFactory
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerURI);

            // Create a Connection
            Connection connection = connectionFactory.createConnection();
            connection.start();

            connection.setExceptionListener(this);

            // Create a Session
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            // Create the destination (Topic or Queue)
            Destination destination = session.createQueue(queueName);

            // Create a MessageConsumer from the Session to the Topic or Queue
            MessageConsumer consumer = session.createConsumer(destination);

            consumer.setMessageListener(this);
            while(!isStop)
            {
            	Thread.sleep(5000);
            }
           
            consumer.close();            
            session.close();
            connection.close();
        }
        catch (Exception e) {
            System.out.println("Caught: " + e);
            e.printStackTrace();
        }
		System.out.printf("\t[Info] Closing Answer Queue...Bye!\n");
		agent = null;
		
	}

	@Override
	public synchronized void onMessage(Message answer) {
		System.out.printf("\t[Info] Receive Message(%s):\n%s\n", queueName, answer);
		try
		{
			String tid = answer.getStringProperty("TID");
			if(tid.startsWith("S"))
			{
				// Service Heat Beat
				agent.srvmCallBack(tid, ((TextMessage)answer).getText(), answer.getStringProperty("Topic"));
			}
			else
			{
				String resp = null;
				if(answer.getBooleanProperty("Correct"))
				{
					resp = String.format("%s|Pass|-|-|-", answer.getStringProperty("ServiceName"));
				}
				else
				{
					resp = String.format("%s|Fail|%s|%s|%s", answer.getStringProperty("ServiceName"),
							                                 answer.getStringProperty("Category"),
							                                 answer.getStringProperty("Reason"),
							                                 answer.getStringProperty("Suggestion"));
				}
				agent.taskCallBack(tid, resp);
				//agent.taskCallBack(tid, resp);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	@Override
	public void onException(JMSException expt) {
		System.err.printf("\t[Expt] Exception Message:\n%s\n", expt);		
	}
}
